package com.lanou3g.ptp.commons;

import com.lanou3g.ptp.utils.DateUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 创建人: 武奇
 * 创建事件: 2019/8/5
 * 选择一个时间段内的每周几日期
 */
public class DayOfWeekDateHandler implements TaskDateHandler {

    private Date startDate;
    private Date endDate;
    private List<Integer> weekdays;

    public DayOfWeekDateHandler(Date startDate, Date endDate, List<Integer> weekdays) {
        this.startDate = startDate;
        this.endDate = endDate;
        this.weekdays = weekdays;
    }

    @Override
    public List<Date> dispose() {
        List<Date> dateList = new ArrayList<>();
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startDate);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);

        while (!DateUtils.isSameDay(startCalendar, endCalendar)) {
            int dayOfWeek = startCalendar.get(Calendar.DAY_OF_WEEK);
            dayOfWeek -= 1;
            if (weekdays.contains(dayOfWeek)) {
                Date day = startCalendar.getTime();
                dateList.add(day);
            }
            startCalendar.add(Calendar.DATE, 1);
        }

        return dateList;
    }
}
